import request from "@/axios/request";
const { ElMessage } = require("element-plus");

export default {
  name: "Register",
  data() {
    return {
      form: {
        username: "",
        password: "",
        phone: "",
        email: "",
        identity: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请输入电话号码", trigger: "blur" },
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号", trigger: "blur" }
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          { type: "email", message: "请输入正确的邮箱格式", trigger: ["blur", "change"] }
        ],
        identity: [
          { required: true, message: "请选择身份", trigger: "change" }
        ]
      }
    };
  },
  computed: {
    disabled() {
      const { username, password, phone, email, identity } = this.form;
      return Boolean(username && password && phone && email && identity);
    }
  },
  methods: {
    register() {
      this.$refs.form.validate(async valid => {
        if (valid) {
            if (this.form.identity === 'admin') {
                const allowedPhone1 = '19853228572';
                const allowedPhone2 = '13501793363';
                const allowedPhone3 = '18169836217';
                const allowedPhone4 = '19863753121';
                if (this.form.phone !== allowedPhone1
                    && this.form.phone !== allowedPhone2
                    && this.form.phone !== allowedPhone3
                    && this.form.phone !== allowedPhone4
                ) {
                    try {
                        await this.$confirm(
                            '非团队成员，没有管理员注册权限',
                            '提示',
                            {
                                confirmButtonText: '确认',
                                type: 'error',
                                closeOnClickModal: false,
                                closeOnPressEscape: false,
                                lockScroll: true           
                            }
                        );
                    } catch (err) {}
                }
            }
            const urlMap = {
                tenant: "/tenant/register",
                landlord: "/landlord/register",
                admin: "/admin/register"
            };

            const url = urlMap[this.form.identity];

            if (!url) {
                ElMessage.error("未知的身份类型");
                return;
            }

            const { username, password, phone, email } = this.form;

            request.post(url, { username, password, phone, email }).then(res => {
                if (res.code === 200) {
                    ElMessage({
                        message: "注册并登录成功",
                        type: "success",
                    });
                    window.sessionStorage.setItem("userInfo", JSON.stringify(res.data));
                    window.sessionStorage.setItem("identity", JSON.stringify(this.form.identity));
                    this.$router.push("/"+this.form.identity+"Home");
                } else {
                    ElMessage({
                        message: res.msg,
                        type: "error",
                    });
                }
            }).catch(err => {
                ElMessage.error("网络错误，请稍后再试");
                console.error(err);
            });
        }
      });
    }
  }
};